<?php
/*
 * Author :basanas
 */
require_once ('include/css_head.php');
require_once ('include/functions.php');
require_once ('include/header.php');
require_once ('../frameworks/objects/entity.php');
require_once ('../frameworks/objects/attribute.php');
require_once ('../bl/bl.php');

if ( !empty($_POST['entity_ids']) ) {
    $nEntities = count(split(",",$_POST['entity_ids']));
}
else
    $nEntities = 0;

if ( ( isset($_GET['id']) && !is_null($_GET['id']) ) && !empty($_POST['entity_ids']) && $nEntities > 1)
{
    if ( !ctype_digit((string)$_GET['id']) ) {
        UI_registerError('msg_error', "Invalid category request");
        header("location: index.php");
        exit(0);
    }
    $category = BL_getCategory($_GET['id']);
    $attributes = $category->GetAttributes();
    $entities = BL_getEntities(split(",",$_POST['entity_ids']));
    $numAttributes = count($attributes);
    $category_name = $category->GetName();
}
else if( $nEntities == 1 )
{
    UI_registerError('msg_warning', "You cannot select just one entity!");
    UI_registerError('msg_warning', "Please select two or more entities.");
    header("location: browse.php?id=".$_GET['id']);
}
else
{
    UI_registerError('msg_warning', "You haven't selected any entities to compare!");
    UI_registerError('msg_warning', "Please browse a category first and select some entities.");
    header("location: browse.php?id=".$_GET['id']);
}

?>
    <script type="text/javascript" src="js/weightsLib.js"></script>
    <script type="text/javascript" src="js/libs/jquery.tablesorter.min.js"></script>
    <script type="text/javascript" src="js/libs/jquery.tablesorter.pager.js"></script>

    <div style="width: 90%; margin:auto;">
        <div style='padding: 3px; margin-bottom: 5px' class='blockHeader'>Category:
            <a href='browse.php?id=<?php echo $_GET['id']?>'><?php echo $category_name?> (Browse category)</a></div>
            <div id="weightWrapper">
                <table class="blockBlue" style="padding: 5px; border:1px solid black; margin-bottom: 20px">
                    <tr>
                        <th class='tC tCC'>Attributes </th>
                        <th class='tC tCC'>Default Criteria</th>
                        <th class='tC tCC'>Custom criteria</th>
                        <th class='tC tCC'>Weights</th>
                    </tr>
    				<?php
                    $i = 0;
                    foreach ($attributes as $attribute) {
                    ?>
                        <tr>
                            <td class='tC'><?php echo $attribute->GetName(); ?></td>
                            <td class='tC' >
                            <?php
                            /* TODO: Check if it needs fixing for quality attributes */
                            $bestvalue = $attribute->GetBestValue();
                            echo $bestvalue;
                            if (strcmp($bestvalue, "close_to") == 0) {
                                echo $attribute->GetCloseToValue();
                            }
                            ?>
        					</td>

                            <td class='tC'>
                                <!--TODO: Quality attributes don't need such options -->
                                <select id="best<?php echo $i; ?>" name="<?php echo $i; ?>" onchange="javascript:toggleSelect(<?php echo $i; ?>)">
                                <option value="min" <?php if ($bestvalue == "min")
                                    echo "selected = \"selected\""; ?>>Min</option>
                                <option value="max" <?php if ($bestvalue == "max")
                                    echo "selected = \"selected\""; ?>>Max</option>
                                <option value="close_to" <?php if ($bestvalue == "close_to")
                                    echo "selected = \"selected\""; ?>>Close To</option>
                                </select>
                                <?php initCloseTo($i); ?>
                            </td>

                            <td class='tC' >
                                <div id="amount<?php echo $i; ?>">
                                    <div id="slider<?php echo $i; ?>" class="slider"></div>
                                    <span>1</span>
                                    <span style="margin-left:3px;">2</span>
                                    <span style="margin-left:6px;">3</span>
                                    <span style="margin-left:6px;">4</span>
                                    <span style="margin-left:6px;">5</span>
                                    <span style="margin-left:6px;">6</span>
                                    <span style="margin-left:6px;">7</span>
                                    <span style="margin-left:6px;">8</span>
                                    <span style="margin-left:6px;">9</span>
                                    <span style="margin-left:6px;">10</span>
                                </div>
                            </td>
                    <?php
                    $i++;
                    }
                    ?>
                </table>
                <form id="submitWeights" name="submitWeights" method="post" action="ranking.php">
                <input type="button" value="Submit" onclick="javascript:prepareWeights(
                    <?php echo count($attributes); ?>, <?php echo '['.$_POST['entity_ids'].']'?>, <?php echo $_GET['id']?>)"/>
                <input type="hidden" name="weights" />
                <input type="hidden" name="entity_ids" />
                <input type="hidden" name="id" />
                </form>
            </div>
        <br />

        <div id="visMatrix">
            <table id="visMatrixTable" class='tablesorter' style="margin: 10px 0 10px 0;">
                    <thead>
                    <tr>
                    <th class='tC'>Entity name</th>
                    <?php
                    for ($i=0 ; $i < $numAttributes ; $i++)
                    {
                        echo "<th class='tC'>".$attributes[$i]->GetName()."</th>";
                    }
                    ?>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    for ($i=0 ; $i < $nEntities ; $i++)
                    {
                    ?>
                    <tr >
                        <?php
                        echo "<td class='tC'>".$entities[$i]->getName()."</td>";
                        $attribs = $entities[$i]->GetAttributes();
                        for ($j=0 ; $j < $numAttributes ; $j++)
                            echo "<td class='tC'>".$attribs[$j]->GetValue()."</td>";
                        ?>
                    </tr>
                    <?php
                    }
                    ?>
                    </tbody>
            </table>
            <div id="pagerMatrix" class="pager">
                <form>
            		<img src="images/first.png" class="first"/>
            		<img src="images/prev.png" class="prev"/>
            		<input type="text" class="pagedisplay"/>
            		<img src="images/next.png" class="next"/>
            		<img src="images/last.png" class="last"/>
            	</form>
            </div>
        </div>
    </div>

<script type="text/javascript">
$( ".slider" ).each(function(){
    $(this).slider({
		value: 5,
		min: 1,
		max: 10,
		step: 1,
	});
    $(this).css('color', 'red' );
});

$("#visMatrixTable")
.tablesorter({
    sortList: [[0,0]]
})
.tablesorterPager({container: $("#pagerMatrix")});
</script>

<?php
require_once ('include/footer.php');

function initCloseTo($i)
{
    global $bestvalue;

    if (!strcmp($bestvalue, "close_to")) {
        echo '<input style="visibility:visible; height:20px" type="text" id="close_to' .
            $i . '" name="close_to_val" value="' . $attribute->GetCloseToValue() .
            '" maxlength="30" size="16"/>';
    } else
        echo '<input style="visibility:hidden; height:20px" type="text" id="close_to' .
            $i . '" name="close_to_val" maxlength="30" size="16"/>';
}
?>